package com.zzjson.thread.reset;

import java.util.concurrent.TimeUnit;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : 线程中断的复位</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年08月09日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */

public class ThreadResetDemo {

	/**
	 * 两种方式复位
	 * 1. 通过Thread.interrupted();
	 * 2. 通过InterruptedException
	 *
	 * @param args
	 * @throws InterruptedException
	 * @see ExceptionThreadDemo
	 */
	public static void main(String[] args) throws InterruptedException {
		Thread thread = new Thread(() -> {
			//在一个线程类中，存在一个成员属性是 isInterrupt,并且是volitale修饰的,默认是false,判断当前线程是否是中断状态
			while (true) {
				//默认是false
				if (Thread.currentThread().isInterrupted()) {
					System.out.println("before:" + Thread.currentThread().isInterrupted());
					//复位回到初始状态
					Thread.interrupted();
					System.out.println("after:" + Thread.currentThread().isInterrupted());
				}

			}
		});
		thread.start();
		TimeUnit.SECONDS.sleep(1);
		// 把isInterrupt设置为 true
		thread.interrupt();


	}
}